package com.app.controller.article;

import com.app.aop.has.HasRedissonLock;
import com.app.aop.has.HasRole;
import com.app.domain.bo.article.ArticleBo;
import com.app.domain.page.Page;
import com.app.domain.vo.article.ArticleVo;
import com.app.framework.common.R;
import com.app.service.article.ArticleService;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 文章控制器
 */
@RestController
@RequestMapping("/api/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    /**
     * 文章列表
     *
     * @param bo 入参
     */
    @PostMapping("/list")
    public R<Page<ArticleVo>> lists(@RequestBody ArticleBo bo) {
        return R.ok(articleService.lists(bo));
    }

    /**
     * 标签筛选文章
     *
     * @param bo 入参
     */
    @PostMapping("/articleTags")
    public R<Page<ArticleVo>> articleTags(@RequestBody ArticleBo bo) {
        return R.ok(articleService.articleTags(bo));
    }

    /**
     * 热门文章
     */
    @PostMapping("/articleHots")
    public R<Page<ArticleVo>> articleHots(@RequestBody ArticleBo bo) {
        return R.ok(articleService.articleHots(bo));
    }

    /**
     * 文章详情
     *
     * @param articleId 文章ID
     */
    @GetMapping("/details/{articleId}")
    public R<ArticleVo> details(@PathVariable("articleId") Long articleId) {
        return R.ok(articleService.details(articleId));
    }

    /**
     * 添加文章
     *
     * @param bo 入参
     */
    @HasRole()
    @HasRedissonLock("save_article")
    @PostMapping("/save")
    public R<?> saveOne(@RequestBody ArticleBo bo) {
        articleService.saveOne(bo);
        return R.ok(null, "添加成功");
    }

    /**
     * 修改文章
     *
     * @param bo 入参
     */
    @HasRole()
    @HasRedissonLock("update_article")
    @PostMapping("/update")
    public R<?> updateOne(@RequestBody ArticleBo bo) {
        Assert.notNull(bo.getId(), "文章ID不能为空");
        articleService.updateOne(bo);
        return R.ok(null, "修改成功");
    }

    /**
     * 删除文章
     *
     * @param articleId 文章ID
     */
    @HasRole()
    @HasRedissonLock("delete_article")
    @PostMapping("/delete/{articleId}")
    public R<?> deleteOne(@PathVariable("articleId") Long articleId) {
        articleService.deleteOne(articleId);
        return R.ok(null, "删除成功");
    }

}